var errorLicense = true;
var errorModel = true;
var errorDisplacement = true;
var errorBuydate = true;
var errorName = true;
var errorMobile = true;

$(document).ready(function(){
	addFadeEffect();
});

function checkLicense() {
	var license = $("#license").val();
	var pattern = /^[\u4e00-\u9fa5][a-zA-Z0-9]{6}$/;
	
	$("#error_message").html("");
	$("#license").css("background", "#ffffff");
	errorLicense = false;
	if (license == "" || !pattern.test(license)) {
		$("#error_message").html("车牌错误<br/>");
		$("#license").css("background", "#fbe2e2");
		errorLicense = true;
	} else {
		requestUrl = "check_license_number";
		$.post(requestUrl, {license:license}, function(ret) {
			if (ret.code == 0) {
				errorLicense = false;
			} else {
				$("#error_message").html("车牌已存在<br/>");
				$("#license").css("background", "#fbe2e2");
				errorLicense = true;
			}
		}, "json");
	}
	return true;
}

function checkModel() {
	var model = $("#model").val();
	var pattern = /^[\s]+$/;

	if (model == "" || pattern.test(model)) {
		$("#error_message").html("车型错误<br/>");
		$("#model").css("background", "#fbe2e2");
		errorModel = true;
	} else {
		$("#error_message").html("");
		$("#model").css("background", "#ffffff");
		errorModel = false;
	}
	return true;
}

function checkDisplacement() {
	var displacement = $("#displacement").val();
	var pattern = /^[0-9]+[.]?[0-9]+$/;

	if (displacement == "" || !pattern.test(displacement)) {
		$("#error_message").html("排量错误<br/>");
		$("#displacement").css("background", "#fbe2e2");
		errorDisplacement = true;
	} else {
		$("#error_message").html("");
		$("#displacement").css("background", "#ffffff");
		errorDisplacement = false;
	}
	return true;
}

function checkBuydate() {
	var buydate = $("#buy_date").val();
	var pattern = /^[12][0-9]{3}-[0-1][0-9]$/;

	if (buydate == "" || !pattern.test(buydate)) {
		$("#error_message").html("购买年月错误<br/>");
		$("#buy_date").css("background", "#fbe2e2");
		errorBuydate = true;
	} else {
		$("#error_message").html("");
		$("#buy_date").css("background", "#ffffff");
		errorBuydate = false;
	}
	return true;
}

function checkName() {
	var name = $("#name").val();
	var pattern = /^[\s]+$/;

	if (name == "" || pattern.test(name)) {
		$("#error_message").html("客户姓名错误<br/>");
		$("#name").css("background", "#fbe2e2");
		errorName = true;
	} else {
		$("#error_message").html("");
		$("#name").css("background", "#ffffff");
		errorName = false;
	}
	return true;
}

function checkMobile() {
	var mobile = $("#mobile").val();
	var pattern = /^1[0-9]{10}$/;

	if (mobile == "" || !pattern.test(mobile)) {
		$("#error_message").html("手机号码错误<br/>");
		$("#mobile").css("background", "#fbe2e2");
		errorMobile = true;
	} else {
		$("#error_message").html("");
		$("#mobile").css("background", "#ffffff");
		errorMobile = false;
	}
	return true;
}

function addCustomer() {
	if (checkLicense() && !errorLicense
			&& checkModel() && !errorModel
			&& checkDisplacement() && !errorDisplacement
			&& checkBuydate() && !errorBuydate
			&& checkName() && !errorName
			&& checkMobile() && !errorMobile) {
		var requestUrl = "add_customer";
		var params = {
				license:$("#license").val(),
				model:$("#model").val(),
				displacement:$("#displacement").val(),
				buy_date:$("#buy_date").val(),
				name:$("#name").val(),
				mobile:$("#mobile").val(),
				image_name:$("#image_name").val()
		};
		$.post(requestUrl, params, function(ret){
			if (ret.code == 0) {
				addCover(contextPath);
				$("#div_alert").css("visibility", "visible");
			} else {
				$("#error_message").html(ret.message);
			}
		}, "json");
	}
}

function freshImage(data) {
	var result = eval('(' + data + ')');
	var imageName = "";

	if (result.code == "0") {
		imageName = result.resultData;
		document.getElementById("img_merchant").src = WSContextPath + "/image/uploads/" + imageName;
	} else {
		alert(result.message);
	}
	document.getElementById("image_name").value = imageName;
}